package com.orange.common.autoconfig.template;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.orange.common.autoconfig.properties.SendSmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description:    使用阿里云提供的发送手机短信SDK
 * @author: orange
 * @date: 2023/9/22
 * @param
 * @return:
 */
@Slf4j
public class SendPhoneMessageTemplate {
    @Autowired
    private SendSmsProperties sendSmsProperties;
    public SendPhoneMessageTemplate(){

    }

    public void send(String phone,String code) throws Exception {
        log.info("用户的电话号码{},{}",phone,code);
        String accessKeyId = sendSmsProperties.getAccessKey();
        String accessKeySecret= sendSmsProperties.getSecret();

        //配置阿里云
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";

        com.aliyun.dysmsapi20170525.Client client =  new com.aliyun.dysmsapi20170525.Client(config);

        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName(sendSmsProperties.getSignName())
                .setTemplateCode(code)
                .setTemplateParam(sendSmsProperties.getTemplateCode());
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse response = client.sendSms(sendSmsRequest);
        SendSmsResponseBody body = response.getBody();
        System.out.println(body.toString());
    }
}
